import configs from "./configs";
import Koa, {Context} from "koa";
import {bootstrapControllers} from "koa-ts-controllers"
import koaRouter from "koa-router";
import path from "path";
import koaBodyParser from "koa-bodyparser";
import Boom from "@hapi/Boom";
import {Sequelize} from "sequelize-typescript"

(async ()=>{
    const app = new Koa();
    const router = new koaRouter()

    // 链接数据库
    const db =new Sequelize({
        ...configs.datebase,
        models:[__dirname+"/models/**/*"]
    });

    // 注册路由
    await bootstrapControllers(app, {
        router,
        basePath:"/api",
        versions:[1],
        controllers:[
            path.resolve(__dirname, "controllers/**/*")
        ],
        errorHandler:async (err:any, ctx:Context)=>{
            let status = 500;
            let body:any = {
                statusCode: status,
                error: "Internal Server error",
                message: "An internal server error occurred"
            }

            if(err.output){
                status = err.output.statusCode;
                body = {
                    ...err.output.payload
                };
                if(err.data){
                    body.errorDetails = err.data
                }
            }
            ctx.status = status
            ctx.body = body
        }
    })

    // router.all("/*", async ctx =>{
    //     throw Boom.notFound("注册失败","用户名已经被注册了");
    // })
    app.use(koaBodyParser())
    app.use(router.routes())
    
    app.listen(configs.server.port,configs.server.host,()=>{
        console.log(`服务启动成功，${configs.server.host}:${configs.server.port}`);
    })
})()